<?php
/*
 * @Author: 李志刚
 * @CopyRight: 2022-2099 李志刚（854378082@qq.com）
 * @Date: 2019-01-03 20:14:16
 * @Description: 文章管理
 * @LastEditors: 李志刚
 * @LastEditTime: 2022-04-30 16:45:59
 * @FilePath: /CoinCMF/app/Http/Controllers/Console/Content/ArticleController.php
 */

namespace App\Http\Controllers\Console\Content;

use Illuminate\Support\Facades\Validator;
use App\Customize\Func;
use Illuminate\Http\Request;
use App\Models\Content\Article;
use App\Http\Controllers\Controller;
use App\Http\Requests\BatchRequest;
use App\Http\Requests\Cms\ArticleRequest;
use App\Http\Requests\Cms\ArticleUpdateRequest;
use App\Http\Requests\DetailRequest;
use App\Http\Requests\SortRequest;

class ArticleController extends Controller
{
    public function getList(Request $req)
    {
        $page = $req->input('page', 0);
        $size = $req->input('size', 10);
        $catid = $req->input('cateid');
        // 搜索关键字
        $key = $req->input('key', '');
        $starttime = $req->input('starttime');
        $endtime = $req->input('endtime');
        $query = Article::with(['cate' => function ($q) {
            $q->select('id', 'name');
        }])->where(function ($q) use ($catid) {
            if ($catid != '' && $catid != 0) {
                $q->where('cate_id', $catid);
            }
        })->where(function ($q) use ($key) {
            if ($key != '') {
                $q->where('title', 'like', '%' . $key . '%');
            }
        })->where(function ($q) use ($starttime, $endtime) {
            if ($starttime != '' && $endtime != '') {
                $q->where('created_at', '>=', $starttime)->where('created_at', '<=', $endtime);
            }
        })->select('id', 'title', 'cate_id', 'hits', 'publish_at', 'sort', 'push_flag')->where('del_flag', 0);
        $count = $query->count();
        $list = $query->offset(($page - 1) * $size)->limit($size)->orderBy('id', 'desc')->get();
        return $this->resJson(200, '成功', ['list' => $list, 'total' => $count]);
    }
    public function postCreate(ArticleRequest $request)
    {
        $data = $request->all();
        $url = $data['link_flag'] == 1 ? $data['url'] : Func::createUuid();
        $create = ['cate_id' => $data['cate_id'], 'title' => $data['title'], 'content' => $data['content'], 'description' => $data['description'], 'keywords' => $data['keywords'], 'thumb' => $data['thumb'], 'tpl' => $data['tpl'], 'push_flag' => $data['push_flag'], 'source' => $data['source'], 'link_flag' => $data['link_flag'], 'url' => $url, 'sort' => $data['sort']];
        $create['publish_at'] = $data['publish_at'] == '' ? date('Y-m-d H:i:s') : $data['publish_at'];
        Article::create($create);
        return $this->resJson(200, '成功');
    }
    public function getDetail(DetailRequest $request)
    {
        $id = $request->input('detail_id');
        $info = Article::find($id);
        return $this->resJson(200, '成功', $info);
    }
    public function postEdit(ArticleUpdateRequest $request)
    {
        $id = $request->input('detail_id');
        $data = $request->all();
        $update = ['cate_id' => $data['cate_id'], 'title' => $data['title'], 'content' => $data['content'], 'description' => $data['description'], 'keywords' => $data['keywords'], 'thumb' => $data['thumb'], 'tpl' => $data['tpl'], 'push_flag' => $data['push_flag'], 'source' => $data['source'], 'link_flag' => $data['link_flag'], 'sort' => $data['sort']];
        $update['publish_at'] = $data['publish_at'] == '' ? date('Y-m-d H:i:s') : $data['publish_at'];
        if ($data['link_flag']) $update['url'] = $data['url'];
        Article::where('id', $id)->update($update);
        return $this->resJson(200, '成功');
    }
    public function postRemove(DetailRequest $request)
    {
        $detail_id = $request->input('detail_id');
        Article::where('id', $detail_id)->update(['del_flag' => 1]);
        return $this->resJson(200, '成功');
    }
    public function postDeleteAll(BatchRequest $req)
    {
        $ids = $req->input('detail_id', []);
        Article::whereIn('id', $ids)->update(['del_flag' => 1]);
        return $this->resJson(200, '成功');
    }
    public function postSort(SortRequest $request)
    {
        $detail_id = $request->input('detail_id');
        $sort = $request->input('sort');
        Article::where('id', $detail_id)->update(['sort' => $sort]);
        return $this->resJson(200, '成功');
    }
}
